package com.example.gameandroid;

import org.andengine.engine.camera.ZoomCamera;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.scene.Scene;
import org.andengine.opengl.font.Font;
import org.andengine.opengl.font.FontFactory;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.texture.region.ITiledTextureRegion;
import org.andengine.ui.activity.SimpleBaseGameActivity;

import android.graphics.Color;

public class MainActivity extends SimpleBaseGameActivity  {
	
	private int height;
	private int width;
	private ZoomCamera camera;
	private StartScene scene;
	private Font font;
	private GameBD bd;
	private EngineOptions engineOptions;
	
	private ITextureRegion mapaTexture;
	private ITextureRegion rockTexture;
	private ITextureRegion gameOverTelaTexture;
	private ITiledTextureRegion botaoJumpTexture;
	private ITiledTextureRegion playerTexture;
	private ITiledTextureRegion botaoStartTexture;
	private ITiledTextureRegion botaoRestartTexture;
	
	@Override
	public EngineOptions onCreateEngineOptions() {
		
		height = 540;
		width = 960;
		
		camera = new ZoomCamera(0, 0, width, height);
		camera.setBounds(0, 0, Integer.MAX_VALUE , height);
		camera.setBoundsEnabled(true);
		engineOptions = new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, 
				new RatioResolutionPolicy(width, height), camera);		
		
		return engineOptions; 
	}
	

	@Override
	protected void onCreateResources() {
		FontFactory.setAssetBasePath("font/");
		font = FontFactory.createFromAsset(this.getFontManager(), this.getTextureManager(), 512, 512, TextureOptions.BILINEAR, this.getAssets(), 
				"arial.ttf", 32, true, Color.WHITE);
		font.load();
		
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/"); 
		
		BitmapTextureAtlas tela1Atlas = new BitmapTextureAtlas(this.getTextureManager(), 1024, 1024);
		mapaTexture = BitmapTextureAtlasTextureRegionFactory.createFromAsset(tela1Atlas, this, "mapa.png", 0, 0);
		tela1Atlas.load(); 
		
		BitmapTextureAtlas gameOverTelaAtlas = new BitmapTextureAtlas(this.getTextureManager(), 1024, 1024);
		gameOverTelaTexture = BitmapTextureAtlasTextureRegionFactory.createFromAsset(gameOverTelaAtlas, this, "gameover.png", 0, 0);
		gameOverTelaAtlas.load();
		
		BitmapTextureAtlas rockAtlas = new BitmapTextureAtlas(this.getTextureManager(), 64, 64); 
		rockTexture = BitmapTextureAtlasTextureRegionFactory.createFromAsset(rockAtlas, this, "rock.png", 0, 0);
		rockAtlas.load();
		
		BitmapTextureAtlas botaoJumpAtlas = new BitmapTextureAtlas(this.getTextureManager(), 512, 128);     
		botaoJumpTexture = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(botaoJumpAtlas, this, "botaoJump.png", 0, 0, 2, 1);
		botaoJumpAtlas.load();
		
		BitmapTextureAtlas botaoStartAtlas = new BitmapTextureAtlas(this.getTextureManager(), 512, 128);    
		botaoStartTexture = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(botaoStartAtlas, this, "botaoStart.png", 0, 0, 2, 1);
		botaoStartAtlas.load();
		
		BitmapTextureAtlas botaoRestartAtlas = new BitmapTextureAtlas(this.getTextureManager(), 512, 128);    
		botaoRestartTexture = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(botaoRestartAtlas, this, "botaoRestart.png", 0, 0, 2, 1);
		botaoRestartAtlas.load();
		
		BitmapTextureAtlas playerAtlas = new BitmapTextureAtlas(this.getTextureManager(), 512, 128);   
		playerTexture = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(playerAtlas, this, "player.png", 0, 0, 9, 2);
		playerAtlas.load();
	}

	@Override 
	protected Scene onCreateScene() {
		bd = new GameBD(this, "bdGame",null, 1);
		 
		scene = new StartScene(getEngine(), botaoStartTexture, botaoJumpTexture, font, playerTexture, mapaTexture, width,
				height, camera, rockTexture, gameOverTelaTexture, botaoRestartTexture, bd);

		return scene;
	}
}
